Borland Online And The Cobb Group Present:


July, 1995 - Vol. 2 No. 7

Shrinking Windows EXEs and DLLs

As you become more dependent on the ObjectWindows Library (OWL) or other C++ class libraries to speed development of your Windows applications, you'll probably notice that the size of your executables is increasing. This isn't always a problem, since processor speed and hard disk capacity appear to be on a permanent upward curve. For many systems, however, the size and speed of an executable file are still important issues.

Even in situations where these concerns aren't paramount, wouldn't you like to trim excess size off your Windows applications? Who wouldn't, especially if the process were relatively painless?

For some time, advanced users have resorted to several tricks to prevent or remove unnecessary fixup records and other sources of code bloat in the EXE file. In this article, we'll review some of these techniques, and we'll show you how to use the ­Ox command-line options that Borland has added to TLINK 7.0 to reduce the size of your Windows executable files.

Taking bytes out of your EXEs

With the advent of larger, more powerful class libraries, we've all become more tol-erant of excessive file size in our executables. Unfortunately, mixed in with the beneficial factors that contribute to the size of an EXE, you'll find elements that are completely unnecessary.

Extra! Extra! Read all about it!

Several types of unnecessary data appear in some Windows executables: debugging information, unused space due to suboptimum segment or resource alignment, unnecessary export prologues/epilogues, and less-than-ideal code-segment packing. Recently, improved linker technology such as that in the IDE linker and TLINK 7.0 has made it possible to remove much of this baggage. Before the advent of this technology (or if you're still using TLINK 6.0, which ships with Borland C++ 4.0), you needed to use OPTILINK or some other third-party linker to eliminate extra information. Now, however, TLINK is capable of squeezing every last bit of unnecessary space out of your Windows executables.

For some time (since version 3.1), TLINK has been able to reduce unnecessary prologues and epilogues for functions you export from another application or Dynamic Link Library (DLL). If you compile your Windows programs using the -WSE command-line option, the compiler generates the prologue and epilogue code only for those functions that you explicitly mark with the export modifier. In addition, the -WSE option uses smart callbacks for the functions you export, which means you don't have to use the Windows MakeProcInstance() function call for those exported functions.

Using the -WSE option can reduce the size of a complex Windows application significantly. Unfortunately, since this option wasn't available in the version 3.1 Integrated Development Environment (IDE), many Borland C++ programmers don't realize it's there in versions 4.0 and 4.5.

To take advantage of code-segment packing, you'll want to increase the default packing size of 8192 using the -P command-line option. We suggest using a packing size of 65535. Creating segments this large has several benefits, such as reducing problems related to segment alignment and, in many cases, allowing the linker to use "near" function calls instead of "far" calls. Adding -P:65535 to the linker command-line parameters will force the linker to combine code segments into segments roughly 64KB in size.

Dial -Ox for optimization

The ­WSE and ­P options have been available for several versions of Borland C++. However, beginning with TLINK 7.0, Borland has added the ­Ox series of linker options, shown in Table A. To learn more about how these options reduce the size of your Windows executables, see the article How the -Ox TLINK options reduce EXE size below.

Table A: The new -Ox linker options
Option Action
-Oc Chain fixups
-Oi Iterate data
-Oa Minimize segment-alignment value
-Or Minimize resource-alignment value

Unfortunately, the ­Ox optimizations aren't directly available from the IDE linker. You'll have to use TLINK, the command-line linker, to take advantage of these weight-reducing options in the IDE. If you use the IDE to maintain your projects, see Borland C++ 4.5 - Running TLINK from the IDE, to learn how you can use the ­Ox options from the IDE.

Since the IDE doesn't provide an interface to the new -Ox linker options, you may be tempted to use BCC.EXE, the command-line compiler, instead. Unfortunately, BCC.EXE doesn't recognize these options either. Instead of passing the options to the linker, BCC strips off the "O" portion of the option, causing the linker to misinterpret the new optimizations as -c, -i, -a, and -r. (The first two are valid compiler options, but they're not the ones we want. The last two aren't valid command-line options and will generate errors. )

To solve this problem, you'll need to create a TLINK configuration file to pass the options to the linker. If you place this configuration file (which you must name TLINK.CFG) in the project's main directory, TLINK will load the file and use any of the options it contains as if you had entered them from the command line.

Honey, I shrunk the code!

To see how you can take advantage of the new -Ox optimizations by using the command-line tools, let's build the WHELLO example project. Begin by entering

cd\bc45\examples\windows\whello

From this directory, enter

bcc -W whello

to launch the command-line compiler and automatically link the resulting object file.

When the linker finishes linking the EXE file, enter DIR *.EXE to determine the size of the resulting EXE file. The size of the EXE file should be 31,292 bytes.

Now, let's create a new configuration file for TLINK. Enter EDIT TLINK.CFG to launch the DOS editor and create a new file. When the EDIT screen appears, enter

/Oc
/Oi
/Oa
/Or

When you finish, choose Save from the File menu, and then choose Exit to return to DOS.

Now, rebuild the application by entering

bcc -W whello

This time, when the linker begins creating the EXE file, notice that the message

optimizing........

appears as part of the linker's output.

Again, enter DIR *.EXE to check the size of the EXE file. This time, you'll notice that the EXE's size is now 28,426 bytes, approximately 90 percent of its original size. Since all the other compiler and linker options remain the same, this difference represents the true benefit of the new ­Ox optimizations.

Conclusion

The new -Ox optimization options allow you to trim excess code from your Windows applications. If you're using the command-line tools, you'll need to use a configuration file as we described in this article to pass these options to TLINK.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.